package his.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

import his.entity.Register;
import his.exp.DataAccessException;
import his.util.JdbcUtil;

public class menzhendoctorDaoImpl implements menzhendoctorDao{
	private Connection con=null;
	@Override
	public void setConnection(Connection con) {
		this.con=con;	
	}
	
	@Override
	public List<Register> findRegister(int CaseNumber, String RealName) throws DataAccessException {
		String sql=null;
		if(RealName==null) {
			//根据病历号查询患者数据
			sql="select * from register where CaseNumber=?";
		}else {
			//根据患者名字查询患者数据
			sql="select * from register where RealName like '%"+RealName+"%'";
		}
		//创建PreparedStatement
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		List<Register> allregister=new ArrayList();
		try {
			pstmt = con.prepareStatement(sql);
			if(RealName==null) {
				pstmt.setInt(1,CaseNumber);
			}
			//执行sql
			rs=pstmt.executeQuery();
			while(rs.next()){
				Register register=new Register();
				register.setID(rs.getInt(1));
				register.setCaseNumber(rs.getString(2));
				register.setRealName(rs.getString(3));
				register.setGender(rs.getInt(4));
				register.setIDnumber(rs.getString(5));
				register.setBirthDate(rs.getDate(6));
				register.setAge(rs.getInt(7));
				register.setAgeType(rs.getString(8));
				register.setHomeAddress(rs.getString(9));
				register.setVisitDate(rs.getDate(10));
				register.setNoon(rs.getString(11));
				register.setDeptID(rs.getInt(12));
				register.setUserID(rs.getInt(13));
				register.setRegisLeID(rs.getInt(14));
				register.setSettleID(rs.getInt(15));
				register.setIsBook(rs.getString(16));
				register.setRegisTime(rs.getTimestamp(17));
				register.setRegisterID(rs.getInt(18));
				register.setVisitState(rs.getInt(19));
				allregister.add(register);
			}	
		}catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException(e.getMessage());
		}finally{
			JdbcUtil.release(null, pstmt,rs);
		}
		return allregister;
	}
}
